package com.example.project;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;



import android.location.LocationManager;
import android.net.Uri;
import android.support.v4.app.NotificationCompat;
import android.util.Log;

public class ProximityIntentReceiver extends BroadcastReceiver {
	public static final String NOTIFICATION_DATA = "NOTIFICATION_DATA";
   // private static final int NOTIFICATION_ID = 1000;
	/**TODO: Fix it**/
Context c;
    @Override
    public void onReceive(Context context, Intent intent) {
        
        String key = LocationManager.KEY_PROXIMITY_ENTERING;
        c = context;
        Boolean entering = intent.getBooleanExtra(key, false);
        
        if (entering) {
            Log.d(getClass().getSimpleName(), "entering");
            createNotification(0, "hem entrat a la zona");
        }
        else {
            Log.d(getClass().getSimpleName(), "exiting");
        }
        
    } 
        
     
    
    private void createNotification(long when,String data){
		String notificationContent ="ALERT";
		String notificationTitle ="hem entrat a la zona de proximitat";
		//large icon for notification,normally use App icon
		Bitmap largeIcon = BitmapFactory.decodeResource(c.getResources(),R.drawable.tobi);
		int smalIcon =R.drawable.tobi;
		String notificationData="This is data : "+data;
		
		/*create intent for show notification details when user clicks notification*/
		Intent intent =new Intent(c.getApplicationContext(), Menjar.class);
		intent.putExtra(NOTIFICATION_DATA, notificationData);
		
		/*create unique this intent from  other intent using setData */
		intent.setData(Uri.parse("content://"+when));
		/*create new task for each notification with pending intent so we set Intent.FLAG_ACTIVITY_NEW_TASK */
		PendingIntent pendingIntent = PendingIntent.getActivity(c.getApplicationContext(), 0, intent, Intent.FLAG_ACTIVITY_NEW_TASK);
		
		/*get the system service that manage notification NotificationManager*/
		NotificationManager notificationManager =(NotificationManager) c.getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE); 
				
		/*build the notification*/
		NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(
				c.getApplicationContext())
				.setWhen(when)
				.setContentText(notificationContent)
				.setContentTitle(notificationTitle)
				.setSmallIcon(smalIcon)
				.setAutoCancel(true)
				.setTicker(notificationTitle)
				.setLargeIcon(largeIcon)
				.setDefaults(Notification.DEFAULT_LIGHTS| Notification.DEFAULT_VIBRATE| Notification.DEFAULT_SOUND)
				.setContentIntent(pendingIntent);
		
		/*Create notification with builder*/
		Notification notification=notificationBuilder.build();
		
		/*Fix Notification*/
		notification.flags=Notification.FLAG_ONGOING_EVENT;
		
		/*sending notification to system.Here we use unique id (when)for making different each notification
		 * if we use same id,then first notification replace by the last notification*/
		notificationManager.notify((int) when, notification);
	}
    
}